﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


class SaveStudents
{
    static void AddStudentToFile(Student student)
    {
        BinaryWriter writeToFile = new BinaryWriter(new FileStream("students.bin", FileMode.Append));
        using (writeToFile)
        {
            writeToFile.Write(student.FirstName);
            writeToFile.Write(student.LastName);
            writeToFile.Write(student.FacNumber);
        }
    }

    static void AddStudentToFile(string fileName, Student student)
    {
        BinaryWriter writeToFile = new BinaryWriter(new FileStream(fileName, FileMode.Append));
        using (writeToFile)
        {
            writeToFile.Write(student.FirstName);
            writeToFile.Write(student.LastName);
            writeToFile.Write(student.FacNumber);
        }
    }

    static void PrintAllStudents()
    {
        if (File.Exists("students.bin"))
        {
            BinaryReader readFromFile = new BinaryReader(new FileStream("students.bin", FileMode.Open));
            using (readFromFile)
            {
                while (readFromFile.PeekChar() != -1)
                {
                    Student currentStudent = new Student(readFromFile.ReadString(), readFromFile.ReadString(), readFromFile.ReadInt32());
                    Console.WriteLine(currentStudent.ToString());
                }
            }
        }
        else
        {
            throw new IOException("File doesn't exist!");
        }
    }

    static void DeleteByFacultyNumber(int facNumber)
    {
        if (File.Exists("students.bin"))
        {
            BinaryReader readFromFile = new BinaryReader(new FileStream("students.bin", FileMode.Open));
            string tempFileName = Path.GetTempFileName();
            using (readFromFile)
            {
                while (readFromFile.PeekChar() != -1)
                {
                    Student currentStudent = new Student(readFromFile.ReadString(), readFromFile.ReadString(), readFromFile.ReadInt32());
                    if (currentStudent.FacNumber != facNumber)
                    {
                        AddStudentToFile(tempFileName, currentStudent);
                    }
                }
            }
            File.Copy(tempFileName, "students.bin",true);
            File.Delete(tempFileName);
        }
        else
        {
            throw new IOException("File doesn't exist!");
        }
    }
    
    static void Main()
    {
        if (File.Exists("students.bin"))
        {
            File.Delete("students.bin");
        }
        AddStudentToFile(new Student("Kamen", "Ivanov", 123));
        AddStudentToFile(new Student("Andey", "Andreev", 456));
        AddStudentToFile(new Student("Alexander", "Vyrbanov", 789));
        AddStudentToFile(new Student("Ivan", "Todorov", 012));
        AddStudentToFile(new Student("Vasil", "Raev", 234));
        AddStudentToFile(new Student("Milen", "Ne znam", 345));
        AddStudentToFile(new Student("Petyr", "Pak ne znam", 567));        
        DeleteByFacultyNumber(123);
        PrintAllStudents();
    }
}

